<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    header {
      width: 375px;
      height: 60px;
      font-size: 20px;
      background-color: lightcoral;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  </style>
</head>
<body>
  <div id="app">
    <h2>依赖注入2</h2>
    <grand-father></grand-father>
  </div>

  <script src="../vue3.js"></script>
  <script>
    const {createApp, computed} = Vue;


    const Son = {
      inject: {
        count: {
          from: "count"
        },
        fn: {
          from: "fn"
        }
      },
      template: `
        <div>这是孙子组件 - {{ count }}</div>
        <button @click="fn(123)">btn</button>
      `
    }

    const Father = {
      components: {
        Son,
      },
      template: `
        <div>这是爸爸组件</div>
        <son></son>
      `
    }

    const GrandFather = {
      components: {
        Father,
      },
      template: `
        <div>这是爷爷组件</div>
        <father></father>
      `,
    }

    const app = createApp({
      data() {
        return {
          count: 100
        }
      },
      methods: {
        fn(n) {
          console.log("fn", n);
        }
      },
      components: {
        GrandFather
      },
      provide() {
        return {
          count: this.count,
          fn: this.fn,
        }
      }
    });

    app.mount("#app");
  </script>
</body>
</html>